<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>网站用户名验证</title>
</head>
<body>
    <input type="text" name="userName" placeholder="由字母数字下划线组成最多8位最少5位">
</body>
<script>
    const input = document.querySelector(`[name = "userName"]`)
    input.addEventListener('keyup',function(){
        const reg = /^\w{5,8}$/
        console.log(reg.test(this.value))
    })
</script>
</html>